package com.example.demo.controller;


import com.example.demo.common.BusinessException;
import com.example.demo.common.ErrorEnum;
import com.example.demo.config.myannotation.code.LogAnno.LogAnno;
import com.example.demo.entity.GitHub;
import com.example.demo.outstyle.Message;
import com.example.demo.outstyle.TableData;
import com.example.demo.service.GitHubService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Api(tags = "github")
@Controller
@RequestMapping(value = "/logic/github")
@CrossOrigin
public class GitHubController {
    @Autowired
    GitHubService gitHubServiceImp;

    @ApiOperation(value = "插入信息")
    @PostMapping("/add")
    @ResponseBody
    @LogAnno(operatType = "插入信息")
    public Message add(GitHub gitHub) {
        Message msg = new Message();
        Integer result = gitHubServiceImp.add(gitHub);
        if (result > 0){
            msg.setState(0);
            msg.setMsg("添加成功");
        }else{
            throw new BusinessException(ErrorEnum.E_shanchushibai);
        }
        return msg;
    }
    @ApiOperation(value = "分页查询")
    @PostMapping("/select")
    @ResponseBody
    @LogAnno(operatType = "分页查询")
    public TableData<GitHub> search(int page, int limit,GitHub gitHub) {
        TableData<GitHub> msg = new TableData();
        List<GitHub> languageList = gitHubServiceImp.search(page, limit,gitHub);
        int allCount = gitHubServiceImp.searchCount(gitHub);
        msg.setState(0);
        msg.setCount(allCount);
        msg.setData(languageList);
        msg.setMsg("成功加载");
        return msg;
    }

    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    @ResponseBody
    @LogAnno(operatType = "删除")
    public Message delete(int id) {
        Message msg = new Message();
        Integer result = gitHubServiceImp.delete(id);
        if (result > 0){
            msg.setState(0);
            msg.setMsg("删除成功");
        }else{
            throw new BusinessException(ErrorEnum.E_shanchushibai);
        }
        return msg;
    }

    @ApiOperation(value = "更改")
    @PostMapping("/update")
    @ResponseBody
    @LogAnno(operatType = "更改")
    public Message modify(GitHub gitHub) {
        Message msg = new Message();
        Integer result = gitHubServiceImp.update(gitHub);
        if (result > 0){
            msg.setState(0);
            msg.setMsg("修改成功");
        }else{
            throw new BusinessException(ErrorEnum.E_xiugaishibai);
        }
        return msg;
    }


}
